package com.qianfeng.xinyihezi.xinyihezi.jsondecode;

import android.util.Log;

import com.qianfeng.xinyihezi.xinyihezi.model.DetailData;
import com.qianfeng.xinyihezi.xinyihezi.model.DetailSonCategory;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Tcoy on 2016/11/14.
 */

public class DecodeDetailClassift {
    private String TAG = getClass().getSimpleName();
    private List<DetailSonCategory> detailSonCategoryList;
    private List<DetailData> detailDataList;

    public DecodeDetailClassift() {
    }

    public List<DetailData> decodeDetailClassift(String json) {
        try {
            detailDataList = new ArrayList<>();
            JSONObject jsonObject = new JSONObject(json);
            JSONArray jsonArrayData = jsonObject.optJSONArray("data");
            for (int i = 0; i < jsonArrayData.length(); i++) {//六月     最热门分类
                JSONObject jsonObjectItem = (JSONObject) jsonArrayData.get(i);
                JSONArray jsonArraySonCategory = jsonObjectItem.optJSONArray("son_category");
                detailSonCategoryList = new ArrayList<>();
                for (int j = 0; j < jsonArraySonCategory.length(); j++) {
                    JSONObject jsonObjectSonCategory = (JSONObject) jsonArraySonCategory.get(j);
                    DetailSonCategory detailSonCategory = new DetailSonCategory(jsonObjectSonCategory.optString("cat_id"), jsonObjectSonCategory.optString("image_url"), jsonObjectSonCategory.optString("name"));
                    detailSonCategoryList.add(detailSonCategory);
                }
                Log.i(TAG, "decodeDetailClassift: " + jsonObjectItem.optString("image_url"));
                DetailData detailData = new DetailData(jsonObjectItem.optString("cat_id"), jsonObjectItem.optString("image_url"), jsonObjectItem.optString("name"), detailSonCategoryList);
                detailDataList.add(detailData);
            }
            return detailDataList;
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return null;
    }
}
